#include "hdf_dlist.h"
#include <stdio.h>
#include <stdlib.h>


typedef struct Student
{
    struct DListHead node;
    char name[50];
    int age;
}Student;

//头插法生成双向链表
bool test_dlist_insert_head()
{
	struct DListHead head;
	//初始化双向链表
	DListHeadInit(&head);
	Student s1 = { {NULL,NULL},"lucy",20 };
  Student s2 = { {NULL,NULL},"lily",21 };
  Student s3 = { {NULL,NULL},"tom",22 };
	//插入链表
	DListInsertHead(&s1.node,&head);
	DListInsertHead(&s2.node,&head);
	DListInsertHead(&s3.node,&head);
	Student *s = (Student *)(head.next); 
	//遍历双向链表
	DLIST_FOR_EACH_ENTRY(s,&head,Student,node)
	{
        printf("name: %s , age: %d\n", s->name, s->age);
	}
	int count = DListGetCount(&head);
	printf("count=%d\n",count);
	return true;
}

//尾插法生成双向链表
bool test_dlist_insert_tail()
{
	struct DListHead head;
	//初始化双向链表
	DListHeadInit(&head);
	Student s1 = { {NULL,NULL},"lucy",20 };
  Student s2 = { {NULL,NULL},"lily",21 };
  Student s3 = { {NULL,NULL},"tom",22 };
	//插入链表
	DListInsertTail(&s1.node,&head);
	DListInsertTail(&s2.node,&head);
	DListInsertTail(&s3.node,&head);
	Student *s = (Student *)(head.next); 
	//遍历双向链表
	DLIST_FOR_EACH_ENTRY(s,&head,Student,node)
	{
        printf("name: %s , age: %d\n", s->name, s->age);
	}
	return true;
}

//删除一个节点
bool test_dlist_remove()
{
	struct DListHead head;
	//初始化双向链表
	DListHeadInit(&head);
	Student s1 = { {NULL,NULL},"lucy",20 };
  Student s2 = { {NULL,NULL},"lily",21 };
  Student s3 = { {NULL,NULL},"tom",22 };
	//插入链表
	DListInsertTail(&s1.node,&head);
	DListInsertTail(&s2.node,&head);
	DListInsertTail(&s3.node,&head);
	Student *s = (Student *)(head.next); 
	//删除S2节点前
	printf("删除S2节点前\n");
	//遍历双向链表
	DLIST_FOR_EACH_ENTRY(s,&head,Student,node)
	{
        printf("name: %s , age: %d\n", s->name, s->age);
	}
	//删除节点s2
	DListRemove(&s2.node);
	//删除S2节点后
	//遍历双向链表
	printf("删除S2节点后\n");
	s = (Student *)(head.next);
	DLIST_FOR_EACH_ENTRY(s,&head,Student,node)
	{
        printf("name: %s , age: %d\n", s->name, s->age);
	}
}

//合并两个双向链表
bool test_dlist_merge()
{
	struct DListHead head1;
	struct DListHead head2;
	//初始化双向链表1
	DListHeadInit(&head1);
	DListHeadInit(&head2);
	
	Student s1 = { {NULL,NULL},"lucy",20 };
  Student s2 = { {NULL,NULL},"lily",21 };
  Student s3 = { {NULL,NULL},"tom",22 };
	
	Student s4 = { {NULL,NULL},"小白",24 };
  Student s5 = { {NULL,NULL},"小红",26 };
  Student s6 = { {NULL,NULL},"小黑",23 };
	
	//插入链表
	DListInsertTail(&s1.node,&head1);
	DListInsertTail(&s2.node,&head1);
	DListInsertTail(&s3.node,&head1);
	
	DListInsertTail(&s4.node,&head2);
	DListInsertTail(&s5.node,&head2);
	DListInsertTail(&s6.node,&head2);
	
	//合并两个双向链表
	if(!DListIsEmpty(&head1)&& !DListIsEmpty(&head2))
	{
		DListMerge(&head1,&head2);
	}
	
	//遍历双向链表
	printf("合并两个双向链表后\n");
	Student *s = (Student *)(head2.next);
	DLIST_FOR_EACH_ENTRY(s,&head2,Student,node)
	{
        printf("name: %s , age: %d\n", s->name, s->age);
	}
	
}

int main()
{
	//test_dlist_insert_head();
	//test_dlist_insert_tail();
	//test_dlist_merge();
  test_dlist_remove();
	return 0;
}